<template>
  <div class="imageupload">
    <!-- 上传组件 -->
    <el-upload
      v-if="!imgUrl"
      action=""
      list-type="picture-card"
      :before-upload="beforeUpload"
      :http-request="httpRequest"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <div v-else class="myimg">
      <img v-errImage="baseImg" @click="previewImage" :src="imgUrl" alt="" />
      <i @click="close" class="el-icon-close"></i>
    </div>
    <!-- 进度条 -->
    <el-progress
      v-if="progerssshow"
      style="width: 150px;margin-top:20px"
      :show-text="false" 
      :percentage="percentage"
    ></el-progress>
    <!-- 预览的对话框 -->
    <el-dialog title="图片预览" :visible.sync="showDialog">
      <div class="prebox">
        <img class="preimg" :src="imgUrl" alt="" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import COS from "cos-js-sdk-v5";
export default {
  //外部传入的图片地址
  props: ["imgUrl"],
  data() {
    return {
      percentage:0,
      progerssshow: false,//进度条
      showDialog: false,//预览弹窗
      cos: null,//定义上传到存储桶的对象
      // 默认不正常时的备胎
      baseImg:require("@/assets/common/bigUserHeader.png"),
    };
  },
  methods: {
    //预览图片
    previewImage() {
      this.showDialog = true;
    },
    //删除图片
    close() {
      this.$emit("update:imgUrl", "");
    },
    //自定义逻辑
    httpRequest(file) { 
      console.log('file',file);
      //打开进度条
      this.progerssshow = true;
      //得到图片资源
      const myfile = file.file;
      //上传到腾讯云中
      this.cos.putObject(
        {
          Bucket: "liangyi-1306777848" /* 必须 */,
          Region: "ap-guangzhou" /* 存储桶所在地域，必须字段 */,
          Key: myfile.name /* 必须 */,
          StorageClass: "STANDARD",
          Body: myfile, // 上传文件对象
          onProgress: (progressData) => {
            console.log('progressData',progressData);
            this.percentage = progressData.percent * 100
          },
        },
        (err, data) => {
          //err:上传出错的错误信息
          //data:上传成功后的返回值
          if (err) {
            this.$message.error(err.message);
            return;
          }
          this.$emit("update:imgUrl", "http://" + data.Location);
          setTimeout(() => {
            //关闭进度条
            this.progerssshow = false;
          }, 500);
        }
      );
    },
    //上传照片
    beforeUpload(file) {
      // 判断类型
      const isType = file.type === "image/jpeg" || file.type === "image/png";
      if (!isType) {
        this.$message.error("对不起图片格式不正确");
      }
      //判断大小
      const isSize = file.size / 1024 / 1024 < 2;
      if (!isSize) {
        this > $message.error("图片大小于不超过2M");
      }
      return isType && isSize;
    },
  },
  created() {
    this.cos = new COS({
      SecretId: "AKID5ybx2rPgfBVZNvEH4buxkGXYJp7B0auz", // 身份识别 ID
      SecretKey: "qQq43mGUSAcomVyCAsKWTBdplX3JJ3Yn", // 身份密钥
    });
  },
};
</script>

<style lang="scss" scoped>
.imageupload {
  .myimg {
    width: 150px;
    height: 150px;
    position: relative;
    img {
      width: 150px;
      height: 150px;
    }
    i {
      position: absolute;
      top: 0;
      right: 0%;
      width: 15px;
      height: 20px;
      line-height: 20px;
      font-size: 12px;
      background-color: rgba(0, 0, 0, 0.5);
      color: #fff;
      border-radius: 0 0 0 15px;
      display: none;
    }
    &:hover i {
      display: block;
      cursor: pointer;
    }
  }
  .prebox {
    text-align: center;
    .preimg {
      width: 200px;
      height: 200px;
    }
  }
}
</style>